<template>
  <div class="app-container">
    <PlatformHeader />
    <div class="main-container" :style="{ height: `calc(100vh - 72px)` }">
      <!-- 左侧面板 -->
      <div
        class="left-panel"
        :class="{ 'hidden': leftPaneSize === 0 }"
        :style="{ width: `${leftPaneSize}%` }"
      >
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入机构名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </div>

      <!-- 右侧主内容区 -->
      <div class="right-panel">
        <button
          class="toggle-btn"
          @click="toggleLeftPane"
          :style="{ left: leftPaneSize === 0 ? '10px' : `${leftPaneSize}%` }"
        >
          <i class="el-icon-s-fold" v-if="leftPaneSize > 0"></i>
          <i class="el-icon-s-unfold" v-else></i>
        </button>

        <el-col style="height: 100%;display: flex;flex-direction: column;">
          <el-form
            :model="form"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="90px"
            :rules="formRules"
            class="custom-form"
          >
            <el-form-item label="" prop="deptId" required>
              <el-input
                v-model="selectedNodeInfo"
                placeholder="请从左侧选择机构"
                title="请从左侧选择机构"
                readonly
                clearable
                style="width: 220px;background-color: transparent;"
                @focus="handleInputFocus"
              />
            </el-form-item>

            <el-form-item label="" prop="selectedDate" required>
              <el-date-picker
                popper-class="custom-select-dropdown"
                v-model="form.selectedDate"
                style="width: 220px"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择日期"
                title="请选择日期"
                @change="handleDateChange"
                :picker-options="pickerOptions"
              />
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click="handleQuery"
                class="search-btn action-btn"
                :disabled="!isFormValid"
              >查询</el-button>
              <el-button icon="el-icon-refresh" size="mini" class="reset-btn action-btn" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <!-- 表格容器占满剩余空间 -->
          <div class="table-container">
            <el-table
              v-loading="loading"
              :data="pagedTestPeopleList"
              @selection-change="handleSelectionChange"
              class="custom-table"
              style="width: 100%"
            >
              <el-table-column
                prop="agegroupname"
                label="年龄段"
                align="center"
                width=""
              />
              <el-table-column
                prop="towncountry"
                label="城乡分类"
                align="center"
                width=""
              />
              <el-table-column
                prop="manvalue"
                label="男性"
                align="center"
                width=""
              />
              <el-table-column
                prop="womanvalue"
                label="女性"
                align="center"
                width=""
              />
              <el-table-column
                label="总计"
                align="center"
                width=""
              >
                <template slot-scope="scope">
                  {{ parseInt(scope.row.manvalue || 0) + parseInt(scope.row.womanvalue || 0) }}
                </template>
              </el-table-column>
            </el-table>
          </div>

          <!-- 分页器 -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            class="pagination"
          />
        </el-col>
      </div>
    </div>
  </div>
</template>

<script>
import { deptTreeSelect } from "@/api/system/user";
import PlatformHeader from '../../../components/PlatformHeader/index.vue';
import { gettestlogforday } from "@/api/monitormanage/testpeoplelog";

export default {
  name: "Testpeople",
  components: { PlatformHeader },
  data() {
    const validateDate = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请选择日期'));
      } else {
        callback();
      }
    };
    // 获取今天日期并格式化为 YYYY-MM-DD
    const getToday = () => {
      const today = new Date();
      const year = today.getFullYear();
      const month = String(today.getMonth() + 1).padStart(2, '0');
      const day = String(today.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    };

    const today = getToday();
    return {
      leftPaneSize: 0,
      form: {
        deptId: undefined,
        selectedDate: today,
        starttime: today,
        endtime: today,
        testtime:today
      },
      loading: false,
      showSearch: true,
      deptOptions: undefined,
      deptName: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      formRules: {
        deptId: [
          { required: true, message: '请选择机构', trigger: ['change', 'blur'] }
        ],
        selectedDate: [
          { validator: validateDate, trigger: ['change', 'blur'] }
        ]
      },
      selectedNodeInfo: '',

      testPeopleList: [],
      pagedTestPeopleList: [],
      ids: [],
      single: true,
      multiple: true,

      // 分页相关
      currentPage: 1,
      pageSize: 10,
      total: 0,

      pickerOptions: {
        disabledDate: (time) => {
          return time.getTime() > Date.now();
        },
        // shortcuts: [{
        //   text: '今天',
        //   onClick(picker) {
        //     picker.$emit('pick', new Date());
        //   }
        // }, {
        //   text: '昨天',
        //   onClick(picker) {
        //     const date = new Date();
        //     date.setTime(date.getTime() - 3600 * 1000 * 24);
        //     picker.$emit('pick', date);
        //   }
        // }, {
        //   text: '一周前',
        //   onClick(picker) {
        //     const date = new Date();
        //     date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
        //     picker.$emit('pick', date);
        //   }
        // }]
      }
    };
  },
  computed: {
    isFormValid() {
      return this.form.deptId && this.form.selectedDate;
    }
  },
  watch: {
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
        this.form.deptId = response.data[0].id
        this.selectedNodeInfo = response.data[0].label
      });
    },
    toggleLeftPane() {
      this.leftPaneSize = this.leftPaneSize === 0 ? 16 : 0;
    },
    handleInputFocus() {
      this.leftPaneSize = 16;
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    handleNodeClick(data) {
      this.form.deptId = data.id;
      this.selectedNodeInfo = `${data.label} `;
      this.leftPaneSize = 0;
      this.$refs.queryForm.clearValidate(['deptId']);
    },
    handleDateChange(val) {
      if (val) {
        this.form.starttime = val;
        this.form.endtime = val;
        this.form.testtime = val;
        this.$refs.queryForm.clearValidate(['selectedDate']);
      } else {
        this.form.starttime = undefined;
        this.form.endtime = undefined;
        this.form.testtime = undefined;
      }
    },

    // 分页处理
    handleSizeChange(val) {
      this.pageSize = val;
      this.updatePagedData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.updatePagedData();
    },
    updatePagedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      this.pagedTestPeopleList = this.testPeopleList.slice(start, end);
    },

    handleQuery() {
      this.$refs.queryForm.validate(valid => {
        if (valid) {
          this.loading = true;
          const queryParams = {
            deptid: this.form.deptId,
            starttime: this.form.starttime,
            endtime: this.form.endtime,
            testtime: this.form.testtime,
            year: this.form.starttime.substring(0, 4),
          };

          gettestlogforday(queryParams).then(response => {
            this.testPeopleList = response;
            this.total = this.testPeopleList.length;
            this.updatePagedData();
            this.loading = false;
          }).catch(() => {
            this.loading = false;
          });
        }
      });
    },
    resetQuery() {
      this.form = {
        deptId: undefined,
        selectedDate: null,
        starttime: undefined,
        endtime: undefined,
        testtime: undefined,
      };
      this.selectedNodeInfo = '';
      this.testPeopleList = [];
      this.pagedTestPeopleList = [];
      this.total = 0;
      this.currentPage = 1;
      this.$refs.tree.setCurrentKey(null);
      this.$nextTick(() => {
        this.$refs.queryForm.clearValidate();
      });
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
  },
  created() {
    this.getDeptTree();
  }
};
</script>

<style scoped lang="scss">
/* 保持原有样式不变 */
.app-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: url('../../assets/images/bgn.png');
  background-size: 100% 100%;
  padding: 16px;
  box-sizing: border-box;
  padding-top: 0;
}

.main-container {
  position: relative;
  width: 100%;
  overflow: hidden;
  padding: 10px;
  padding-top: 20px;
}

.left-panel {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  background-color: #0160FA;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
  z-index: 10;
  transition: width 0.3s ease;
  overflow: auto;
  padding: 16px;
  box-sizing: border-box;
}

.left-panel.hidden {
  width: 0 !important;
  padding: 0;
}

.right-panel {
  width: 100%;
  height: 100%;
  padding-left: 0;
  transition: padding-left 0.3s ease;
  box-sizing: border-box;
  overflow: auto;
}

.toggle-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 10px;
  height: 30px;
  background-color: rgba(0,128,255,0.3);
  border: 1px solid #0080ff;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  transition: left 0.3s ease;
}

.toggle-btn i {
  color: #fff;
  font-size: 10px;
}

.custom-form {
  .el-form-item {
    margin-right: 20px;
    margin-bottom: 18px;
  }

  .el-form-item__label {
    white-space: nowrap;
    padding-right: 8px;
    color: #fff !important;
  }

  .el-input, .el-select, .el-date-editor {
    width: 220px;
  }

  .el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: 220px;
  }
}

::v-deep .el-tree {
  background: #0080FF;
  color: #fff;
  border-radius: 8px;
}

::v-deep .el-tree-node__content:hover {
  background-color: #0879e9;
}

::v-deep .el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  background-color: #0879e9;
}

::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  border-radius: 4px;
  background-color: rgba(240, 240, 240, 0.3);
}

::-webkit-scrollbar-thumb {
  border-radius: 8px;
  background-color: rgba(0, 128, 255, 0.5);
}

::-webkit-scrollbar-thumb:hover {
  background-color: rgba(0, 128, 255, 0.7);
}

::v-deep .el-input .el-input__inner {
  background-color: rgba(255, 255, 255, 0.1);
  border: 1px solid #00CCFE;
  border-radius: 6px;
  color: #f0f0f0;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
  font-size: 14px;
  /*height: 36px !important;
  line-height: 36px !important;*/
}

::v-deep .el-input__inner::placeholder {
  color: #fff !important;
}

::v-deep .el-input .el-input__inner:focus {
  border-color: #409eff;
  box-shadow: 0 0 5px rgba(64, 158, 255, 0.6);
  background-color: rgba(255, 255, 255, 0.15);
  color: #fff;
}

::v-deep .el-form-item .el-button {
  border-radius: 22px;
  font-weight: 600;
  font-size: 15px;
  padding: 10px 28px;
  transition: all 0.3s ease;
  box-shadow: 0 3px 8px rgba(66, 133, 244, 0.3);
}

::v-deep .el-form-item .el-button--primary {
  background: linear-gradient(45deg, #409eff, #66b1ff);
  border: none;
  color: #fff;
  box-shadow: 0 5px 14px rgba(64, 158, 255, 0.6);
}

::v-deep .el-form-item .el-button--primary:hover {
  background: linear-gradient(45deg, #66b1ff, #409eff);
  box-shadow: 0 7px 18px rgba(64, 158, 255, 0.8);
}

::v-deep .el-date-editor {
  background-color: rgba(255, 255, 255, 0.1) !important;
  /*border: 1px solid #00CCFE !important;*/
  border-radius: 6px;
  color: #f0f0f0 !important;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

::v-deep .el-date-editor:hover {
  border-color: #409eff !important;
}

::v-deep .el-date-editor .el-input__inner {
  background-color: transparent !important;
  color: #fff !important;
}

::v-deep .el-date-editor .el-input__inner::placeholder {
  color: #fff !important;
}

/* 表格容器占满空间 */
.table-container {
  flex: 1;
  min-height: 0;
  width: 100%;
  overflow: auto;
}

::v-deep .el-table {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: transparent;
  border-radius: 0px;
  overflow: hidden;
  border: none;
  width: 100%;
}

::v-deep .el-table::before {
  height: 0;
}

::v-deep(.el-table__header-wrapper) .el-table__header th {
  background-color: #f5f7fa;
  font-weight: bold;
  background: #f7e797;
  color: #1e4f91;
  font-size: 18px;
  height: 30px;
  line-height: 30px;
  border: none;
}

::v-deep(.el-table__header .el-table__cell) {
  background-color: #f5f7fa;
}

::v-deep(.el-table th.el-table__cell.is-leaf) {
  border-bottom: none;
}

::v-deep(.el-table .el-table__cell) {
  padding: 8px 0;
}

::v-deep(.el-table__body-wrapper) {
  flex: 1;
  overflow: auto;
  border-radius: 0 0 12px 12px;
}

::v-deep .el-table__body {
  border-collapse: separate !important;
  border-spacing: 0 5px !important;
}

::v-deep .el-table__row {
  td {
    border: none;
    color: #000;
    font-size: 18px;
    padding: 12px 8px;
  }
}

::v-deep .el-table__row:nth-child(odd) {
  background: linear-gradient(90deg, #99e8ff, #4f85ff);
}

::v-deep .el-table__row:nth-child(even) {
  background: linear-gradient(90deg, #4f85ff, #99e8ff);
}

::v-deep .el-table__body tr:hover > td {
  background: #9ef7c5 !important;
}

::v-deep .el-table .cell {
  line-height: 35px;
  height: 35px;
}

::v-deep .el-table--striped .el-table__body tr.el-table__row--striped td.el-table__cell {
  background-color: transparent;
}

/* 分页栏样式 */
.pagination {
  margin-top: 16px;
  padding: 10px 0;
  background-color: transparent;
  border-radius: 8px;
  display: flex;
  justify-content: flex-end;
}

::v-deep .el-pagination {
  color: #fff !important;
}

::v-deep .el-pagination__total,
::v-deep .el-pagination__jump {
  color: #fff !important;
}

::v-deep .el-pagination.is-background .btn-next,
::v-deep .el-pagination.is-background .btn-prev,
::v-deep .el-pagination.is-background .el-pager li {
  background-color: rgba(255, 255, 255, 0.1);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.2);
}
::v-deep .el-pager li{
  background: #1890ff;
  color: #fff;
}
::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #409eff;
  color: #fff;
}

::v-deep .el-pagination.is-background .el-pager li:not(.disabled):hover {
  color: #409eff;
}

::v-deep .el-input__clear {
  color: #a8c0ff;
}

::v-deep .el-input__clear:hover {
  color: #409eff;
}
::v-deep .el-table thead.is-group th.el-table__cell {
  background: #f7e797;
}
::v-deep .el-table .el-table__header-wrapper th, .el-table .el-table__fixed-header-wrapper th {
  background: #f7e797;
  font-size: 20px;
}
::v-deep .el-pagination.is-background .el-pager li{
  background-color:#568eff;
  color: #fff!important;
}
/* 调整表单里的按钮间距 */
::v-deep .el-form-item .el-button {
  margin-left: 12px; /* 你可以改成 16px 更宽松 */
}
/* 按钮通用样式（不改尺寸） */
::v-deep .el-form-item .action-btn {
  border-radius: 20px; /* 圆润 */
  font-weight: 600;
  border: none;
  transition: all 0.3s ease;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15);
}

/* 悬浮时按钮微微上浮 + 光晕 */
::v-deep .el-form-item .action-btn:hover {
  transform: translateY(-2px);
}

/* 搜索按钮 - 宝石蓝渐变 */
::v-deep .el-form-item .search-btn {
  background: linear-gradient(135deg, #b4cdeb, #2f6bcc);
  color: #fff;
}
::v-deep .el-form-item .search-btn:hover {
  background: linear-gradient(135deg, #5aa2f8, #3a8ef6);
  box-shadow: 0 6px 14px rgba(58, 142, 246, 0.5);
}

/* 重置按钮 - 高级灰蓝 */
::v-deep .el-form-item .reset-btn {
  background: linear-gradient(135deg, #df9f3f, #edc73d);
  color: #fff;
}
::v-deep .el-form-item .reset-btn:hover {
  background: linear-gradient(135deg, #ffcc80, #ffb74d);
  box-shadow: 0 6px 14px rgba(255, 183, 77, 0.5);
}
/* 关键样式：调整输入框高度为36px */
::v-deep .custom-form .el-input__inner {
  height: 36px!important;
  line-height: 36px!important;
}
/* 为表格行添加圆角 */
::v-deep .el-table__body tr {
  border-radius: 10px !important;
  overflow: hidden;
  display: table-row;
}

/* 调整第一个和最后一个单元格的圆角 */
::v-deep .el-table__body tr td:first-child {
  border-radius: 10px 0 0 10px !important;
}

::v-deep .el-table__body tr td:last-child {
  border-radius: 0 10px 10px 0 !important;
}

/* 确保表格容器允许溢出显示 */
::v-deep .el-table__body-wrapper {
  overflow: visible !important;
}

/* 表头圆角设置 */
::v-deep .el-table__header th:first-child {
  border-radius: 10px 0 0 10px !important;
}

::v-deep .el-table__header th:last-child {
  border-radius: 0 10px 10px 0 !important;
}

/* 确保表头容器允许圆角显示 */
::v-deep .el-table__header-wrapper {
  overflow: hidden !important;
}
/* 确保表头最后一列的圆角显示，移除右侧边框 */
::v-deep .el-table__header th:last-child {
  border-radius: 0 10px 10px 0 !important;
  border-right: none !important;
}

/* 确保表头容器不遮挡圆角 */
::v-deep .el-table__header-wrapper {
  overflow: visible !important;
}

/* 移除表格的默认边框样式，避免冲突 */
::v-deep .el-table__header th {
  border-right: none !important;
}

/* 为表头添加整体边框，避免最后一列无边界 */
::v-deep .el-table__header {
  border-radius: 10px !important;
  overflow: hidden;
}



</style>
